<template>
  <el-dialog
    :title="!dataForm.id ? '添加品牌' : '修改品牌'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRules" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="logo" prop="logo">
        <!--                <el-input v-model="dataForm.logo" placeholder="logo"></el-input>-->
        <SingleUpload v-model="dataForm.logo"></SingleUpload>
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="dataForm.description" placeholder="请输入说明"></el-input>
      </el-form-item>
      <el-form-item label="是否显示" prop="isShow">
<!--        <el-input v-model="dataForm.isShow" placeholder="请输入0(不显示)/1(显示)"></el-input>-->
        <el-select v-model="dataForm.isShow" placeholder="请选择">
          <el-option
            label="显示"
            :value="1">
          </el-option>
          <el-option
            label="不显示"
            :value="0">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检索首字母" prop="firstLetter">
        <el-input v-model="dataForm.firstLetter" placeholder="请输入检索首字母"></el-input>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
<!--        <el-input v-model="dataForm.sort" placeholder="请输非负整数"></el-input>-->
        <el-select v-model="dataForm.sort" placeholder="请选择">
          <el-option
            label="0"
            :value="0">
          </el-option>
          <el-option
            label="1"
            :value="1">
          </el-option>
          <el-option
            label="2"
            :value="2">
          </el-option>
          <el-option
            label="3"
            :value="3">
          </el-option>
          <el-option
            label="4"
            :value="4">
          </el-option>
          <el-option
            label="5"
            :value="5">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload"

export default {
  components: {
    SingleUpload
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        logo: '',
        description: '',
        isShow: '',
        firstLetter: '',
        sort: ''
      },
      dataRules: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        logo: [
          {required: true, message: 'logo不能为空', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '说明不能为空', trigger: 'blur'}
        ],
        isShow: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('显示状态值不能为空'));
              } else if (!/^0|1$/.test(value)) {
                callback(new Error('显示状态值必须为0或1'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }
        ],
        firstLetter: [
          // {required: true, message: '检索首字母不能为空', trigger: 'blur'}
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('检索首字母不能为空'));
              } else if (!/^[A-Z]$/.test(value)) {
                callback(new Error('检索首字母必须在A-Z之间'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }
        ],
        sort: [
          // {required: true, message: '排序不能为空', trigger: 'blur'}
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('排序值不能为空'));
              } else if (!/^(0|[1-9]\d*)$/.test(value)) {
                callback(new Error('排序值必须为非负整数'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            // url: `http://localhost:9090/commodity/brand/info/${this.dataForm.id}`,
            url: this.$http.adornUrl(`/commodity/brand/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.brand.name
              this.dataForm.logo = data.brand.logo
              this.dataForm.description = data.brand.description
              this.dataForm.isShow = data.brand.isShow
              this.dataForm.firstLetter = data.brand.firstLetter
              this.dataForm.sort = data.brand.sort
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            // url: `http://localhost:9090/commodity/brand/${!this.dataForm.id ? 'save' : 'update'}`,
            url: this.$http.adornUrl(`/commodity/brand/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'logo': this.dataForm.logo,
              'description': this.dataForm.description,
              'isShow': this.dataForm.isShow,
              'firstLetter': this.dataForm.firstLetter,
              'sort': this.dataForm.sort
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
